/***********************************************************************
 * 	Copyright @ 2011 Zhejiang Yutian Technology Co.,Ltd
 ==================================================================
 Project:        LaYouNet
 FileName:       TitleBar.java
 Describtion:    文件描述
 Author:         xufei
 Create Date:    2014-7-16 下午3:08:34
 *********************************************************************/
package com.jason.platform.supervision.widght;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.*;
import android.widget.ImageView;
import com.hzlj.supervision.changsha.R;

/**
 * 标题栏
 *
 * @author xufei
 * @version 1.0
 * @date 2014-7-16 下午3:08:34
 * @copyright YTXT
 */
public class TitleBar extends FrameLayout {

    /**
     * 当前类型
     */
    private int mStyle;
    /**
     * 显示标题文字内容
     */
    private String mDisplayName;
    /**
     * 当前绑定AC
     */
    private Activity mBindActivity;

    private OnClickListener mAddOnClickListener;


    public TitleBar(Context context) {
        super(context);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
        this.mStyle = typedArray.getInt(R.styleable.TitleBar_display_style, Style.MAIN);
        this.mDisplayName = typedArray.getString(R.styleable.TitleBar_displayname);
        typedArray.recycle();

        buildTitleBar();
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private ImageView mBackImageView,mAddImageView;
    private TextView mTitleTextView,mUserNameTextView,mBackTitleTextView;
    private RelativeLayout mMainLayout,mBackMainLayout;

    private void buildTitleBar() {
        addView(LinearLayout.inflate(getContext(), R.layout.widget_title_bar, null));
        if (isInEditMode()) {
            return;
        }
        mBackImageView = (ImageView) findViewById(R.id.header_back_btn);
        mTitleTextView = (TextView) findViewById(R.id.header_title);
        mBackTitleTextView = (TextView) findViewById(R.id.header_back_title);
        mUserNameTextView = (TextView) findViewById(R.id.header_user_name);
        mMainLayout = (RelativeLayout) findViewById(R.id.header_main_layout);
        mBackMainLayout = (RelativeLayout) findViewById(R.id.header_back_main_layout);
        mAddImageView = (ImageView) findViewById(R.id.header_back_add_btn);
        mTitleTextView.setText(mDisplayName);
        mBackTitleTextView.setText(mDisplayName);
        RefreshTitleBar(mStyle);

    }

    public void setUserName(String userName){
        mUserNameTextView.setText(userName);
    }

    public void setAddOnClickListener(OnClickListener listener){
        this.mAddOnClickListener = listener;
        if (mAddOnClickListener != null){
            ((ImageView) findViewById(R.id.header_back_add_btn)).setOnClickListener(mAddOnClickListener);
        }
    }

    public void RefreshTitleBar(int style) {
        mStyle = style;
        mBackMainLayout.setVisibility(GONE);
        mMainLayout.setVisibility(GONE);
        mAddImageView.setVisibility(GONE);
        if (style == Style.MAIN) {
            mMainLayout.setVisibility(VISIBLE);
        } else if (style == Style.BACK) {
            mBackMainLayout.setVisibility(VISIBLE);
        } else if (style == Style.BACK_ADD) {
            mBackMainLayout.setVisibility(VISIBLE);
            mAddImageView.setVisibility(VISIBLE);
        }
        mBackImageView.setTag(mStyle);
        mBackImageView.setOnClickListener(mDefOnClickListener);
    }


    public void setDisplayName(String displayName) {
        this.mDisplayName = displayName;
        TextView title = (TextView) findViewById(R.id.header_title);
        title.setText(mDisplayName);
        TextView backTitle = (TextView) findViewById(R.id.header_back_title);
        backTitle.setText(mDisplayName);
    }

    public void bindActivity(Activity activity) {
        this.mBindActivity = activity;
    }

    private OnClickListener mDefOnClickListener = new OnClickListener() {
        public void onClick(View v) {
            if (v.getId() == R.id.header_back_btn) {
                mBindActivity.finish();
            }

        }
    };

    public static class Style {
        public static int MAIN = 0;
        public static int BACK = 1;
        public static int BACK_ADD = 2;
    }

}
